﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HOBA.SMSSender
{
    public partial class Inbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetMessages();
        }

        private void GetMessages()
        {
            try
            {
                MySoapService service = new MySoapService("http://center.fibosms.com/Service.asmx");
                string response = service.GetCommingSMSList(Session["username"].ToString(), Session["password"].ToString(), "1440");
                AddMessages(response);
            }
            catch (Exception exc)
            {
            }
        }

        private void AddMessages(string xmlContent)
        {
            XmlDocument list = new XmlDocument();
            list.LoadXml(xmlContent);

            // Node for each sms message in response file
            XmlNodeList xmlmessages = list.GetElementsByTagName("SMSLIST");
            // A whole list of messages
            string divMessages = "";
            // Info of individual message
            string divMessage = "";
            if (xmlmessages != null)
            {
                for (int i = 0; i < xmlmessages.Count; i++)
                {
                    XmlNode thismessage = xmlmessages[i];

                    divMessage = "<ul class='";
                    if ((i + 1) % 2 == 0)
                        divMessage += "celleven'";
                    else
                        divMessage += "cellodd'";

                    divMessage += " onclick='showThis_Inbox(this);'>";

                    divMessage += "<li>" + thismessage["PhoneNumber"].InnerText + "</li>"; // Sender's phone number
                    divMessage += "<li>" + thismessage["serviceTypename"].InnerText + "</li>";
                    divMessage += "<li>" + thismessage["message"].InnerText + "</li>";
                    divMessage += "<li>" + thismessage["createddate"].InnerText + "</li>";
                    divMessage += "<li>" + thismessage["ServiceTypeID"].InnerText + "</li>";
                    divMessage += "<li>";

                    switch (thismessage["ClientCommingSMSStatus"].InnerText)
                    {
                        case "0":
                            divMessage += "Just received";
                            break;
                        case "1":
                            divMessage += "Processing";
                            break;
                        case "2":
                            divMessage += "Confirmed";
                            break;
                        case "3":
                            divMessage += "Wrong syntax";
                            break;
                        case "4":
                            divMessage += "Expired";
                            break;
                        case "5":
                            divMessage += "No client response";
                            break;
                    }
                    divMessage += "</li></ul>";
                    divMessages += divMessage;
                }
                messages.InnerHtml = divMessages;
            }
        }
    }
}
